#region Using

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

#endregion

public partial class album : BlogEngine.Core.Web.Controls.BlogBasePage
{
	/// <summary>
	/// Method to delete photo
	/// </summary>
	protected void Delete(Guid guid)
	{
		Album album = BlogEngine.Core.Album.GetById(guid);
		album.Delete();
		album.Save();
	}

	/// <summary>
	/// Assignes the selected theme to the pages.
	/// </summary>
	protected override void OnPreInit(EventArgs e)
	{
		base.OnPreInit(e);

		if (!Page.IsPostBack && !string.IsNullOrEmpty(Request.QueryString["delete"]))
		{
			if (Page.User.Identity.IsAuthenticated)
			{
				Delete(new Guid(Request.QueryString["delete"]));
				Response.Redirect("~/");
			}
		}
	}

	protected void Page_Init(object sender, EventArgs e)
	{
		if (!Page.IsPostBack && !Page.IsCallback)
		{
			if (Request.RawUrl.Contains("?id="))
			{
				Guid id = new Guid(Request.QueryString["id"]);
				Album album = Album.GetById(id);
				if (album != null)
				{
					Response.Clear();
					Response.StatusCode = 301;

					int page = GetPageIndex();
					Response.AppendHeader("location", album.RelativeLinkWithPage(page));

					Response.End();
				}
			}
		}

		if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
		{
			Guid id = new Guid(Request.QueryString["id"]);
			this.Album = Album.GetById(id);

			if (Album != null)
			{
				if (!this.Album.IsVisible && !Page.User.Identity.IsAuthenticated)
					Response.Redirect(Utils.RelativeWebRoot + "error404.aspx", true);

				string path = "~/themes/" + BlogSettings.Instance.Theme + "/AlbumView.ascx";

				AlbumView albumView = (AlbumView)LoadControl(path);
				albumView.Album = Album;
				albumView.Location = ServingLocation.SinglePost;

				pwAlbum.Controls.Add(albumView);
				//related.Album = this.Album; 
				//CommentView1.Album = Album; 

				int page = GetPageIndex();
				string url = "~/album.aspx?id=" + Album.Id + "&page={0}";

				hlNext.HRef = string.Format(url, page + 1);
				hlPrev.HRef = string.Format(url, page - 1);

				if (page == 1)
					hlPrev.Visible = false;
				else
					(Page as BlogBasePage).AddGenericLink("prev", "Previous page", hlPrev.HRef);

				if (hlNext.Visible)
					(Page as BlogBasePage).AddGenericLink("next", "Next page", hlNext.HRef);

				Page.Title = Album.Title;
				AddMetaKeywords();
				AddMetaDescription();
				AddGenericLink("last", Album.Last.Title, Album.Last.RelativeLink.ToString());
				AddGenericLink("first", Album.First.Title, Album.First.RelativeLink.ToString());

				if (Album.Next != null)
					base.AddGenericLink("next", Album.Next.Title, Album.Next.RelativeLink.ToString());

				if (Album.Previous != null)
					base.AddGenericLink("prev", Album.Previous.Title, Album.Previous.RelativeLink.ToString());

				Response.AppendHeader("x-pingback", "http://" + Request.Url.Authority + Utils.RelativeWebRoot + "pingback.axd");
			}
		}
	}

	/// <summary>
	/// Retrieves the current page index based on the QueryString.
	/// </summary>
	private int GetPageIndex()
	{
		int index = 0;
		if (!int.TryParse(Request.QueryString["page"], out index))
			return 1;

		if (index == 0)
			return 1;

		return index;
	}

	/// <summary>
	/// Adds the post's description as the description metatag.
	/// </summary>
	private void AddMetaDescription()
	{
		if (!string.IsNullOrEmpty(Album.Description))
			base.AddMetaTag("description", Album.Description);
		else
			base.AddMetaTag("description", BlogSettings.Instance.Description);
	}

	/// <summary>
	/// Adds the post's tags as meta keywords.
	/// </summary>
	private void AddMetaKeywords()
	{
		if (Album.Tags.Count > 0)
		{
			string[] tags = new string[Album.Tags.Count];
			for (int i = 0; i < Album.Tags.Count; i++)
			{
				tags[i] = Album.Tags[i];
			}
			base.AddMetaTag("keywords", string.Join(",", tags));
		}
	}

	public Album Album;

}
